﻿begin;

select d.*
into tmp_weatherdata
from weatherdata d, 
(select max(entry_datetime), max(id) id
from weatherdata
group by primary_id) x
where d.id = x.id;

delete from weatherdata;

insert into weatherdata (select * from tmp_weatherdata);

drop table tmp_weatherdata;

commit;